const msgSecurity = require('../msgSecurity/index');
const cloud = require('wx-server-sdk');
const crypto = require('crypto');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
exports.main = async (event, context) => {
  // 获取基础信息
  const wxContext = cloud.getWXContext();
  let {OPENID}=cloud.getWXContext();
  const db=cloud.database();
  const _=db.command;
  const sid=event.sid;//传参 会话号
  const msg = event.msg;//传参 消息内容 
  const sendType = event.sendType;//传参 发送的消息类型
  const secevent={msg:msg.content,type:sendType};
  switch(sendType){
    case 'text':{
      //安全检测
      const secResult = await msgSecurity.main(secevent, context);
      if(secResult.errCode!=0) {
        return {
          errCode:1,
          errMsg:"文字内容安全检测失败",
          secResult:secResult
        }
      }else if(secResult.result.label!=100){
        let errMsg='';
        switch (secResult.result.label.toString()) {
          case '10001':
            errMsg="含有广告";
            break;
          case '20001':
            errMsg="含有时政";
            break;
          case '20002':
            errMsg="含有色情";
            break;
          case '20003':
            errMsg="不要骂人";
            break;
          case '20006':
            errMsg="要遵纪守法哦";
            break;
          case '20008':
            errMsg="莫要欺诈";
            break;
          case '20012':
            errMsg="不搞低俗活动";
            break;
          case '20013':
            errMsg='涉及版权';
            break;          
          default:
            errMsg="其他不良信息"
            break;
        }
        return {
          errCode:2,
          errMsg:errMsg
        };
      }
      //检测过关
      await db.collection('chat_msg')
      .where({session_id: sid})
      .update({
            data:{
              msg_set:_.push(msg)
            }
      })
      return {errCode:0};
    }
    case 'image':{
        //安全检测
        const secResult = await msgSecurity.main(secevent, context);
        if(secResult.errCode!=0){
          return secResult;
        }
        const hash = crypto.createHash('md5');
        hash.update(msg.content, 'utf8');
        const md5 = hash.digest('hex');
        //上传
        let upData = await cloud.uploadFile({
          cloudPath: 'chat-image/'+md5+'.png',
          fileContent: Buffer.from(msg.content,'base64')
        })
        //返回fileID
        let fileID = upData.fileID;  
        const obj={
          createTimeStamp:Date.now(),
          creator: OPENID,
          content: fileID ,
          type:'image'
        }
        //往chat_msg中推送消息，content是FileID
        await db.collection('chat_msg')
        .where({session_id: sid})
        .update({
          data:{
            msg_set:_.push(obj)
          }
        }) 
        return secResult;
    }
    case 'video':{
      const obj = {
        createTimeStamp:Date.now(),
        creator: OPENID,
        content: msg ,
        type:'video'
      }
      return db.collection('chat_msg')
      .where({session_id: sid})
      .update({
        data:{
          msg_set:_.push(obj)
        }
      })
    }
    case 'file':{
      const obj = {
        createTimeStamp:Date.now(),
        creator: OPENID,
        content: msg ,
        type:'file'
      }
      return db.collection('chat_msg')
      .where({session_id: sid})
      .update({
        data:{
          msg_set:_.push(obj)
        }
      })
    }
    case 'audio':{
      const obj = {
        createTimeStamp:Date.now(),
        creator: OPENID,
        content: msg ,
        type:'voice'
      }
      return db.collection('chat_msg')
      .where({session_id: sid})
      .update({
        data:{
          msg_set:_.push(obj)
        }
      })
    }
  }
    

}